using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WorkOrder.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WorkOrder.Contexts;
using WorkOrder.Interfaces;
using System.Text.Json;
using WorkOrder.Models;
using WorkOrder.Data;
using MyEncrytExensions;

namespace WorkOrder.Extensions;

    public static class ServiceCollectionExtensions
    {

        public static IServiceCollection AddWordOrderDataService(this IServiceCollection services, IConfiguration configuration)
        {
        
            var encryptString = configuration.GetConnectionString("WorkOrderConnection") ?? throw new InvalidOperationException("故障未能提供工单数据库连接字符串");
            // Console.WriteLine("WorkOrder\n"+"Data Source=./WorkOrder.db".EncryptText()) ;
            var databaseType = configuration["DatabaseType"] ?? throw new InvalidOperationException("故障未能提供数据库类型");
 
             var x="server=localhost;uid=WDN;pwd=000135WDN;database=Work;port=3306".EncryptText();
            var y= "server=localhost;uid=WDN;pwd=000135WDN;database=identity;port=3306".EncryptText();
            switch (databaseType.ToLower())
            {
              case "sqlite":
            services.AddDbContextFactory<WorkOrderContext>(options =>
                options.UseSqlite(encryptString.DecryptText(), sqliteOptions =>
                {
                    sqliteOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
                }));
            break;
        case "mysql":
            services.AddDbContextFactory<WorkOrderContext>(options =>
                options.UseMySql(encryptString.DecryptText(), new MySqlServerVersion(new Version(8, 0, 23)), mysqlOptions =>
                {
                    mysqlOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
                }));
            break;
            }

            
            services.AddTransient<IWorkOrderService,WorkOrderService>();
            services.AddTransient<DashboardService>();
            return services;
        }

 
    }
